Page({
  data: {
  
    guest: {
      name: '',
      idcard:'',
      phone: '',
      age:'',
      address:'',
      beadhouse:''
    },

    gender: ['男', '女'],
    gender_value: '',

  },
  getname:function(e){
    this.data.guest.name = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },
  getage:function(e){
    this.data.guest.age = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },
  getidcard:function(e){
    this.data.guest.idcard = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },
  getphone:function(e){
    this.data.guest.phone = e.detail.value
    this.setData({
      guest:this.data.guest
    })

  },
  getaddress:function(e){
    this.data.guest.address = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },
  getnursing:function(e){
    this.data.guest.beadhouse = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },

    // 输入框获取焦点事件监听
    inputFocus: function (e) {
      var id = e.currentTarget.id;
      var string = id + '_focus';
      this.setData({
        [string]: true
      })
    },

  // 用正则表达式校验姓名
  checkName: function (name) {
    var reg = /^[\u4E00-\u9FA5A-Za-z]+$/;
    return this.check(reg, name, '输入的姓名有误');
  },
 

  // 用正则表达式校验
  check: function (reg, data, errorMsg) {
    if (data == '' || !reg.test(data)) { // 输入为空或者非法时，提醒用户
      wx.showToast({
        title: errorMsg,
        icon: 'none',
        duration: 3000
      })
      return false;
    }
    return true;
  },
  
  //性别选择
  bindPickerGenderChange(e) {
    this.setData({
      gender_value: e.detail.value,
    });
  },


  //用正则表达式校验身份证
  checkIdcard:function(idcard) {
    var reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    return this.check(reg, idcard, '输入的身份证号码有误');
  },


  // 用正则表达式校验电话
  checkPhone: function (phone) {
    var reg = /^(((13)|(15)|(17)|(18))\d{9})$/;
    return this.check(reg, phone, '输入的电话有误');
  },

  //用正则表达式校验身份证
  checkAge:function(age) {
    var reg = /^(?:[1-9][0-9]?|1[01][0-9]|120)$/;
    return this.check(reg, age, '输入的年龄有误');
  },

  //跳转去老人个人页面
  Submit:function(e){
    var guest = this.data.guest
    if (!this.checkName(guest.name)) {
      wx.showToast({
        title: '姓名输入有误！',
        icon: 'none'
      })
      return;
    }

    /*识别问题 */
    if (!this.checkPhone(guest.phone)) {
      wx.showToast({
        title: '电话输入有误！',
        icon: 'none'
      })
      return;
    }
    if (!this.checkIdcard(guest.idcard)) {
      wx.showToast({
        title: '身份证号码输入有误！',
        icon: 'none'
      })
      return;
    }
    if (!this.checkAge(guest.age)) {
      wx.showToast({
        title: '年龄输入不合法！',
        icon: 'none'
      })
      return;
    }
    
    var formData={
      oldid: wx.getStorageSync('openid'),
      oldname :guest.name,
      age:guest.age,
      gender :this.data.gender[this.data.gender_value],
      idcard: guest.idcard,
      oldnumber : guest.phone,
      address : guest.address,
      nursinghome:guest.beadhouse
  }
  console.log(formData)
  wx.request({
    url: 'http://127.0.0.1:8082/demo1/superadmin/insertolder',
    data:JSON.stringify(formData),
    method:'POST',
    header:{
      'Content-Type': 'application/json'
    },
    success:(res)=>{
      console.log(res)
      wx.setStorageSync('identity', 'old')
      wx.switchTab({
        url: '/pages/person/person',
      })
    }
  })

    
  }
});
